
import { createReducer } from 'redux-immutablejs'
import { fromJS } from 'immutable'
import {
  GET_GROUP_LIST,GET_CATE_LIST,SELECTED_CATE
} from '../constants/shop'



export const groupList = createReducer(fromJS([]), {
  [GET_GROUP_LIST]: (state, action) => {
    return fromJS(action.data||[])
  }
})



/*
value: 唯一值
label: 显示文本
disabled:是否能选择
isleaf:是否是叶子，默认值为false
 */
export const cateList = createReducer(fromJS([]), {
  [GET_CATE_LIST]: (state, action) => {
    let data=action.data 
    data.map((item,i) =>{
      item.value=item.category_id
      item.label=item.category_name
      item.disabled=item.is_show==1?false:true 
      item.isLeaf=item.children.length ==0?true:false 
      item.children.map((jtem,j) =>{
        jtem.value=jtem.category_id
        jtem.label=jtem.category_name
        jtem.disabled=jtem.is_show==1?false:true
      })
    })
    return fromJS(data)
  }
})

export const selectedCate = createReducer(fromJS({}), {
  [SELECTED_CATE]: (state, action) => {
    localStorage.setItem('selectedCate',JSON.stringify(action.data));
    return fromJS(action.data)
  }
})

